//
//  GQMessageController.m
//  GuQu
//
//  Created by HELIUM STAR on 15/5/7.
//  Copyright (c) 2015年 HELIUM STAR. All rights reserved.
//

#import "GQMessageController.h"
#import "GQMessageTitleView.h"
#import "GQNewsController.h"
#import "GQPrivateChatControler.h"
#import "GQInteractionController.h"

@interface GQMessageController () <GQMessageTitleViewDelegate, UIScrollViewDelegate>

@property (strong, nonatomic) GQMessageTitleView *titleView;
@property (strong, nonatomic) UIScrollView *scrollView;
@property (assign, nonatomic) NSInteger index;

@end

@implementation GQMessageController

- (void)viewDidLoad {
    [super viewDidLoad];
    GQMessageTitleView *GQMT = [[GQMessageTitleView alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    GQMT.item = @[@"消息", @"互动", @"私聊"];
    GQMT.delegate = self;
    self.navigationItem.titleView = GQMT;
    self.titleView = GQMT;
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kToolBarHeight)];
    scrollView.contentSize = CGSizeMake(kScreenWidth * GQMT.item.count, 0);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.bounces = NO;
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
    self.scrollView = scrollView;
    [self.view addSubview:scrollView];
    
    //创建自控制器
    
    GQNewsController *GQNC = [[GQNewsController alloc] init];
    GQNC.view.frame = CGRectMake(0, 0, scrollView.width, scrollView.height);
    
    GQInteractionController *GQIC = [[GQInteractionController alloc] init];
    GQIC.view.frame = CGRectMake(scrollView.width, 0, scrollView.width, scrollView.height);
    
    GQPrivateChatControler *GQPC = [[GQPrivateChatControler alloc] init];
    GQPC.view.frame = CGRectMake(scrollView.width * 2, 0, scrollView.width, scrollView.height);
    
    [scrollView addSubview:GQNC.view];
    [scrollView addSubview:GQIC.view];
    [scrollView addSubview:GQPC.view];
    [self.view addSubview:scrollView];
    [self addChildViewController:GQNC];
    [self addChildViewController:GQIC];
    [self addChildViewController:GQPC];
    
}

- (void)messageTitleView:(GQMessageTitleView *)titleView didClick:(NSInteger)index
{
    self.scrollView.contentOffset = CGPointMake(kScreenWidth * index, 0);
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger pageIndex = scrollView.contentOffset.x / kScreenWidth + 0.5;
    if (_index != pageIndex) {
        [self.titleView setSelectedIndex:pageIndex];
        _index = pageIndex;
    }
}

@end
